package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.UserLoginContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserLoginBean;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/20/2019 21:06
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class UserLoginPresenter extends BasePresenter<UserLoginContract.Model, UserLoginContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public UserLoginPresenter(UserLoginContract.Model model, UserLoginContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 查询用户信息
     */
    public void qurUserInfo(){
        mModel.qurUserInfo().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<UserBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<UserBean> userBeanResultBean) {
                        if (!AppUtil.isSuccess(userBeanResultBean))
                        {
                            mRootView.showMessage(userBeanResultBean.getMessage());
                        }else {
                            DataHelper.saveDeviceData(mAppManager.getCurrentActivity(),AppConstant.USER_INFO,userBeanResultBean.getData());
                            mRootView.qurUserInfoSuccess(userBeanResultBean.getData());
                        }
                    }
                });
    }
    /**
     * 用户登录
     * @param userName
     * @param pwd
     */
    public void userLogin(String userName,String pwd){
        mModel.userLogin(userName,pwd).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<UserLoginBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<UserLoginBean> userBeanResultBean) {
                          if (!AppUtil.isSuccess(userBeanResultBean))
                          {
                              mRootView.showMessage(userBeanResultBean.getMessage());
                          }else {
                              DataHelper.saveDeviceData(mAppManager.getCurrentActivity(),AppConstant.LOGUSER_LOGIN_INFOIN,userBeanResultBean.getData());
                              qurUserInfo();
                          }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
